package com.colin.log.util;

import com.alibaba.fastjson.JSONArray;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

/**
 * 日志工具类，统一创建日志对象
 */
public class LogUtil {
    private static final Logger logger = LoggerFactory.getLogger(LogUtil.class);

    public static final String LOG_PARAM_TRACE_ID = "traceId";
    public static final String LOG_PARAM_CLIENT_IP = "clientIp";
    public static final String LOG_PARAM_URI = "uri";
    
    public static void info(String message){
        logger.info(message);
    }
    
    public static void info(String message, Object... params){
        logger.info(message, params);
    }

    public static void error(String message){
        logger.error(message);
    }

    public static void error(String message, Throwable e){
        logger.error(message, e);
    }

    /**
     * 选择性打印日志
     * 过滤大对象如：MultipartFile,MultipartFile[]
     */
    public static void printSelectiveLog(String pre, Object[] args){
        
        List<Object> argList = Arrays.stream(args)
                .filter(arg -> !(arg instanceof ServletRequest)
                        && !(arg instanceof ServletResponse)
                        && !(arg instanceof MultipartFile)
                        && !(arg instanceof MultipartFile[])
                )
                .collect(Collectors.toList());
        
        if(pre != null && !"".equals(pre)){
            info(pre, JSONArray.toJSONString(argList));
        }else{
            info(JSONArray.toJSONString(argList));
        }
    }
}
